Skip to main content

Convert InputStream to String in Java

Banner java icon

πŸŽ‰ Converting InputStream to String in Java - The Fun Way! πŸš€β€‹

Ever found yourself wrestling with an InputStream and thinking, "I wish this was a String already!"? Well, fret no more! Today, we're diving into multiple ways to convert an InputStream into a String like a Java ninja. πŸ₯·πŸ’»


1️⃣ Using InputStream.readAllBytes() (Since Java 9) πŸ”₯​

If you're using Java 9+, you're in luck! The readAllBytes() method turns your stream into a byte array faster than you can say "Stream!" πŸ“œπŸ’¨

InputStream in = new FileInputStream(new File("C:/temp/test.txt"));

String fileContent = new String(in.readAllBytes());

⚠️ Warning: Avoid using this for files larger than 2GB unless you enjoy OutOfMemoryErrors! 🚨


2️⃣ Using BufferedReader - The Old Reliable πŸ“–β€‹

Ah, BufferedReader... The grandparent of Java file reading. Super efficient and still going strong! πŸ‹οΈβ€β™‚οΈ

InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
String fileContent = out.toString();
reader.close();

Or, if you're feeling fancy with Java 8, go for this sleek, stream-based approach: 🌊

InputStream in = new FileInputStream(new File("C:/temp/test.txt"));
String newLine = System.getProperty("line.separator");

String fileContent;
try (Stream<String> lines = new BufferedReader(new InputStreamReader(in)).lines()) {
fileContent = lines.collect(Collectors.joining(newLine));
}

3️⃣ Google Guava IO - The Modern Magic βœ¨β€‹

If you love Google's open-source tools, Guava has some great utilities! πŸš€

3.1 Dependencies πŸ“¦β€‹

Add this to your pom.xml:

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>

3.2 Using ByteSource πŸ“œβ€‹

InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt"));

ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return inputStream;
}
};

String fileContent = byteSource.asCharSource(Charsets.UTF_8).read();

3.3 Using CharStreams πŸŽ­β€‹

InputStream inputStream = new FileInputStream(new File("C:/temp/test.txt"));
String fileContent;

try (final Reader reader = new InputStreamReader(inputStream)) {
fileContent = CharStreams.toString(reader);
}
System.out.println(fileContent);

4️⃣ Apache Commons IO - The Power Tool βš‘β€‹

If your project already has Apache Commons IO, why not use its magic? πŸͺ„

4.1 Dependencies πŸ“¦β€‹

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>

4.2 Using IOUtils πŸ› οΈβ€‹

Two ways to make it work like a charm! ✨

// Method 1: IOUtils.copy()
StringWriter writer = new StringWriter();
IOUtils.copy(new FileInputStream(new File("C:/temp/test.txt")), writer, "UTF-8");
String fileContent = writer.toString();
System.out.println(fileContent);

// Method 2: IOUtils.toString()
String fileContent = IOUtils.toString(new FileInputStream(new File("C:/temp/test.txt")), "UTF-8");
System.out.println(fileContent);

5️⃣ Java Scanner - The Underdog πŸ€Ήβ€β™‚οΈβ€‹

Not the most famous approach, but hey, it works! Scanner reads tokens from the stream, and in this case, we grab everything in one go. 🎩✨

FileInputStream fin = new FileInputStream(new File("C:/temp/test.txt"));
java.util.Scanner scanner = new java.util.Scanner(fin, "UTF-8").useDelimiter("\\A");

String fileContent = scanner.hasNext() ? scanner.next() : "";
scanner.close();

πŸš€ Wrap-Up​

That’s it, folks! We've covered five awesome ways to convert an InputStream to a String in Java. Pick your favorite and start coding like a pro! 😎πŸ”₯

Happy Coding! πŸŽ‰πŸ±β€πŸ’»